const db = require('../db/index')
const bcrypt = require('bcryptjs')
const sequelize = require('sequelize')
const { Op } = require("sequelize")
//引入模型
const initModels = require('../models/init-models')
const crypto = require("crypto");
const moment = require("moment/moment");
const Company = initModels(db).company_info
const Energy = initModels(db).energy_company
const Apartment = initModels(db).apartment_energy
const Pie = initModels(db).apartment_pie
const Station = initModels(db).station_info

//company_info
exports.addCompany = async (req,res) => {
    //接收表单数据
    const params = req.body
    try{
        await db.sync()
        //生成id
        const id = crypto.randomUUID()
        const moneyTotal = Math.random()*(5000-10000)+10000
        const income = Math.random()*(5000-10000)+10000
        const taxRevenue = Math.random()*(income*0.2-income*0.8)+income*0.8
        const cost = Math.random()*(income*0.3-income*0.8)+income*0.8
        const personTotal = Math.random()*(80-500)+500
        const debt = Math.random()*(1000-3000)+3000
        const profit = Math.random()*(1000-4000)+4000
        let addResult = await Company.create({
            id, moneyTotal, income, taxRevenue, cost, personTotal, debt, profit,
            ...params
        })
        res.cc('添加成功！',0)
    } catch (error) {
        res.cc(error)
    }
}
//energy_company
exports.addEnergy = async (req,res) => {
    //接收表单数据
    const params = req.body
    try{
        await db.sync()
        //生成id
        const arr = []
        const yearList = ['2023','2024','2025']
        const monthList = ['01','02','03','04','05','06','07','08','09','10','11','12']
        const companyResult = await Company.findAll({
            attributes: ['name'],
            group: 'name'
        })
        for (const company of companyResult) {
            for (const year of yearList) {
                for (const month of monthList) {
                    const id = crypto.randomUUID()
                    const createElectricity =  Math.random()*(200-400)+400
                    const useElectricity =  Math.random()*(200-createElectricity)+createElectricity
                    const carbon =  Math.random()*(200-400)+400
                    const water =  Math.random()*(200-400)+400
                    const charge =  Math.random()*(100-500)+500
                    const chargeCapacity =  Math.random()*(100-500)+500
                    const gas =  Math.random()*(100-500)+500
                    arr.push({
                        id,year,month,createElectricity, useElectricity, carbon, water, charge, chargeCapacity, gas,company:company.name
                    })
                }
            }
        }
        let addResult = await Energy.bulkCreate(arr)
        res.cc('添加成功！',0)
    } catch (error) {
        res.cc(error)
    }
}
//apartment_energy
exports.addApartment = async (req,res) => {
    //接收表单数据
    try{
        await db.sync()
        //生成id
        const arr = []
        const apartmentList = ['蔚蓝卡地亚','‌中海右岸','华润置地天府九里','御都花园别墅', '金林半岛', '府河小区', '二仙桥小区',
        '檀木林国宾府', '瑞祥水岸城', '金域华府‌', '天德华府', '上城生活小区', '利泰春天小区', '昊星·观岭8号','中铁奥维尔一期‌', '绿地中心云玺468五期']
        const floorList = ['1号楼A栋','1号楼B栋','2号楼A栋','2号楼B栋','3号楼A栋','3号楼B栋','4号楼A栋','4号楼B栋']
        const monthList = ['01','02','03','04','05','06','07','08','09','10','11','12']
        const yearList = ['2024','2023']
        for (const year of yearList) {
            for (const month of monthList) {
                for (const apartment of apartmentList) {
                    for (const floor of floorList) {
                        const id = crypto.randomUUID()
                        const electricity = Math.random()*(80-200)+200
                        const water = Math.random()*(80-200)+200
                        const gas = Math.random()*(80-200)+200
                        const estimateElectricity = Math.random()*(electricity-220)+220
                        const estimateWater = Math.random()*(water-220)+220
                        const estimateGas = Math.random()*(gas-220)+220
                        arr.push({id,year,month,apartment,floor,electricity,water,gas,estimateElectricity,estimateWater,estimateGas})
                    }
                }
            }
        }
        let addResult = await Apartment.bulkCreate(arr)
        res.cc('添加成功！',0)
    } catch (error) {
        res.cc(error)
    }
}
//apartment_pie
exports.addPie = async (req,res) => {
    //接收表单数据
    try{
        await db.sync()
        //生成id
        const arr = []
        const apartmentList = ['蔚蓝卡地亚','‌中海右岸','华润置地天府九里','御都花园别墅', '金林半岛', '府河小区', '二仙桥小区',
        '檀木林国宾府', '瑞祥水岸城', '金域华府‌', '天德华府', '上城生活小区', '利泰春天小区', '昊星·观岭8号','中铁奥维尔一期‌', '绿地中心云玺468五期']
        const floorList = ['1号楼A栋','1号楼B栋','2号楼A栋','2号楼B栋','3号楼A栋','3号楼B栋','4号楼A栋','4号楼B栋']
        for (const apartment of apartmentList) {
            for (const floor of floorList) {
                const id = crypto.randomUUID()
                const jkyd = Math.random()*(10-100)+100
                const ktyd = Math.random()*(10-100)+100
                const gqzm = Math.random()*(10-100)+100
                const dtgd = Math.random()*(10-100)+100
                const jtzm = Math.random()*(10-100)+100
                const jdyd = Math.random()*(10-100)+100
                const gsyd = Math.random()*(10-100)+100
                const gqsh = Math.random()*(10-100)+100
                const jtsh = Math.random()*(10-100)+100
                const yys = Math.random()*(10-100)+100
                const jtyq = Math.random()*(10-100)+100
                const lsryq = Math.random()*(10-100)+100
                const result = await Apartment.findAll({
                    attributes:[[sequelize.fn('SUM', sequelize.col('electricity')), 'electricity'],],
                    where:{apartment,floor}
                })
                const result1 = await Apartment.findAll({
                    attributes:[[sequelize.fn('SUM', sequelize.col('water')), 'water'],],
                    where:{apartment,floor}
                })
                const result2 = await Apartment.findAll({
                    attributes:[[sequelize.fn('SUM', sequelize.col('gas')), 'gas'],],
                    where:{apartment,floor}
                })
                const result3 = await Apartment.findAll({
                    attributes:['electricity','water','gas'],
                    where:{apartment,floor,year: '2024',month:{[Op.in]: ['11','12']}},
                    order: [['month','ASC']]
                })
                const result4 = await Apartment.findAll({
                    attributes:['electricity','water','gas'],
                    where:{apartment,floor,year: '2023',month:{[Op.in]: ['11','12']}},
                    order: [['month','ASC']]
                })
                const electricity = result[0].electricity
                const water = result1[0].water
                const gas = result2[0].gas
                const electricityTB = parseFloat(((result3[1].electricity-result4[1].electricity)/result4[1].electricity*100).toFixed(2))
                const electricityHB = parseFloat(((result3[1].electricity-result4[0].electricity)/result4[0].electricity*100).toFixed(2))
                const waterTB = parseFloat(((result3[1].water-result4[1].water)/result4[1].water*100).toFixed(2))
                const waterHB = parseFloat(((result3[1].water-result4[0].water)/result4[0].water*100).toFixed(2))
                const gasTB = parseFloat(((result3[1].gas-result4[1].gas)/result4[1].gas*100).toFixed(2))
                const gasHB = parseFloat(((result3[1].gas-result4[0].gas)/result4[0].gas*100).toFixed(2))
                arr.push({id,apartment,floor,jkyd,ktyd,gqzm,dtgd,jdyd,gsyd,jtzm,gqsh,jtsh,yys,jtyq,lsryq,electricity,water,
                    gas,electricityTB,electricityHB,waterTB,waterHB,gasTB,gasHB})
            }
        }
        let addResult = await Pie.bulkCreate(arr)
        res.cc('添加成功！',0)
    } catch (error) {
        res.cc(error)
    }
}
//station_info
exports.addStation = async (req,res) => {
    //接收表单数据
    try{
        await db.sync()
        //生成id
        const arr = []
        const locationList = ['100.20, 30.10', '100.20, 32.10', '100.37, 29.34', '102.06, 28.37',
        '102.06, 30.67', '102.06, 31.87', '102.48, 27.34', '103.06, 28.67', '103.54, 32.16', '103.12, 30.00',
        '104.20, 30.10', '104.68, 31.47', '105.3, 28.41', '104.3, 31.00', '105.3, 32.42', '106.3965, 31.1263', '106.97, 31.31']
        for (const location of locationList) {
            const power = Math.random()*(22-500)+500
            const total = Math.floor(Math.random()*(20-10+1))+10
            const normal = Math.floor(Math.random()*(total-5+1))+5
            const fault = total - normal
            arr.push({location,power,total,normal,fault})
        }
        let addResult = await Station.bulkCreate(arr)
        res.cc('添加成功！',0)
    } catch (error) {
        res.cc(error)
    }
}
